/*
 * @Author: your name
 * @Date: 2021-05-19 16:11:41
 * @LastEditTime: 2021-05-30 14:39:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /xiangpan-admin/src/pages/chain/addChain/index.tsx
 */ /*
 * @Author: your name
 * @Date: 2021-04-24 16:43:48
 * @LastEditTime: 2021-04-24 17:40:26
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /admin/src/pages/CourseInfo/editCourse/index.tsx
 */
import React, { useEffect } from 'react';
import HeaderForm from '@/components/form';
import { IFormTypeEnum } from '@/components/form/formComponent';
import { Button, Form, FormInstance, Input, Modal, Row, Select } from 'antd';
import {
  add_admin,
  add_chain,
  delete_chain,
  edit_admin,
  edit_chain,
  get_chains,
} from '@/services/pages';
import { useRequest } from 'umi';
import useObj from '@/use/useObj';
import { EPermission, IS_TRUE, SuperList } from '@/enum';
import { globalModel } from '../../globalModel';
import { observer } from 'mobx-react';

interface IProps {
  visible: boolean;
  close: () => void;
  refresh: () => void;
  row?: any;
}

const AccountModal = (props: IProps) => {
  const [obj, setObj] = useObj({
    chainList: [],
    accountInfo: null,
  });
  console.log(SuperList[1], 'globalModel.userInfo?.is_super=');

  const superList = () => {
    if (globalModel.userInfo?.is_super === EPermission.SuperAdmin) {
      return [SuperList[1], SuperList[2]];
    } else if (globalModel.userInfo?.is_super === EPermission.ChainAdmin) {
      return [SuperList[2]];
    } else {
      return [];
    }
  };

  useEffect(() => {
    obj.accountInfo?.is_super === EPermission.ChainAdmin &&
      globalModel.getChains();

    obj.accountInfo?.is_super === EPermission.StoreAdmin &&
      globalModel.getStores();
  }, [obj.accountInfo]);
  useEffect(() => {
    setObj({ accountInfo: props.row });
    form.setFieldsValue(props.row);
  }, [props.row]);

  const onFinish = async (values: any) => {
    await form.validateFields();
    console.log(form, 'form===');
    if (props.row?.chain_id) {
      await edit_admin({ ...values, admin_id: props.row.admin_id });
    } else {
      await add_admin({ store_id: 0, ...values });
    }
    props.close();
    props.refresh();
  };
  console.log(props.row, 'props.row=');
  const [form] = Form.useForm();

  console.log(form.getFieldValue('is_super'), 'form.getFieldValue===');

  return (
    <Modal
      visible={props.visible}
      title={props.row?.chain_id ? '编辑账号' : '添加账号'}
      footer={null}
      maskClosable={false}
      onCancel={props.close}
    >
      <Form
        form={form}
        labelCol={{ span: 4 }}
        onFinish={onFinish}
        onValuesChange={(value, allValues) => {
          setObj({ accountInfo: allValues });
          if (value.is_super) {
            form.setFieldsValue({ chain_id: null, store_id: null });
          }
          console.log(value, 'valie=====');
        }}
      >
        <Form.Item label="账号" name="username" rules={[{ required: true }]}>
          <Input placeholder="请输入" />
        </Form.Item>
        <Form.Item label="密码" name="password" rules={[{ required: true }]}>
          <Input placeholder="请输入" />
        </Form.Item>
        <Form.Item
          label="账号身份"
          name="is_super"
          rules={[{ required: true }]}
        >
          <Select optionFilterProp="label" placeholder="请选择">
            {superList().map((row, index) => {
              return (
                <Select.Option key={index} value={row.value} label={row.label}>
                  {row.label}
                </Select.Option>
              );
            })}
          </Select>
        </Form.Item>
        {obj.accountInfo?.is_super === EPermission.ChainAdmin && (
          <Form.Item
            label="连锁店"
            name="chain_id"
            rules={[{ required: true }]}
          >
            <Select optionFilterProp="label" placeholder="请选择">
              {globalModel.chainList.map((row, index) => {
                return (
                  <Select.Option
                    row={index}
                    value={row.chain_id}
                    label={row.chain_name}
                  >
                    {row.chain_name}
                  </Select.Option>
                );
              })}
            </Select>
          </Form.Item>
        )}

        {obj.accountInfo?.is_super === EPermission.StoreAdmin && (
          <Form.Item label="门店" name="store_id" rules={[{ required: true }]}>
            <Select optionFilterProp="label" placeholder="请选择">
              {globalModel.storeList.map((row, index) => {
                return (
                  <Select.Option
                    row={index}
                    value={row.store_id}
                    label={row.store_name}
                  >
                    {row.store_name}
                  </Select.Option>
                );
              })}
            </Select>
          </Form.Item>
        )}
        <Form.Item>
          <Button htmlType="submit" type="primary" style={{ marginLeft: 80 }}>
            确定
          </Button>
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default observer(AccountModal);
